/**
 * 2009-10-23 下午03:51:21 
 * esignature
 * com.smc.security
 * AbstractAuthenticationToken
 */
package com.smc.security;

/**
 * @author chenheng
 * @version 0.0.1
 * 抽象类用户认证令牌
 */
public abstract class AbstractAuthenticationToken implements Authentication {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -1937899981841144866L;
	/**
	 * 认证用户的其它信息。比如用户端信息。
	 */
	private Object details;
	/**
	 * 用户的授权组信息。用户可能有多个授权。
	 */
	private GrantedAuthority[] authorities ;
	/**
	 * 用户是否成功通过了认证且授权
	 */
	private boolean authenticated = false ;
	/**
	 * 默认构造函数
	 *
	 */
	public AbstractAuthenticationToken() {
		
	}
	/**
	 * 取得授权组信息的构造函数
	 * @param authorities	用户的授权组信息。
	 */
	public AbstractAuthenticationToken(GrantedAuthority[] authorities) {
		this.authorities = authorities ;
	}
	/**
	 * 取得用户的授权信息组
	 */
	public GrantedAuthority[] getAuthorities() {
		if ( authorities == null ) {
			return null;
		}
		return authorities ;
	}
	
	public Object getCredentials() {
		return null;
	}

	public Object getDetails() {
		return details ;
	}
	public void setDetails(Object details) {
		this.details = details ;
	}
	public Object getPrincipal() {
		return null;
	}
	public Object getUUUID() {
		return null;
	}
	public boolean isAuthenticated() {
		return authenticated;
	}
	
	public void setAuthenticated(boolean isAuthenticated)
			throws IllegalArgumentException {
		this.authenticated = isAuthenticated ;
	}
	/**
	 * 取得用户名。
	 */
	public String getName() {
		return (this.getPrincipal() == null ? "" : this.getPrincipal().toString());
	}
	/**
	 * 用户认证授权信息字符串化。
	 */
	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append(super.toString()).append(":");
		sb.append("Username: ").append(this.getPrincipal()).append(";");
		sb.append("Password: [PROTECTED];");
		sb.append("Authenticated: ").append(this.isAuthenticated()).append(";");
		sb.append("Details: ").append(this.getDetails()).append(";");
		if ( this.getAuthorities() != null ) {
			sb.append("Granted Authorities: ");
			for ( GrantedAuthority ga : authorities ) {
				sb.append(ga.toString()).append(",");
			}
		} else {
			sb.append("Not granted any authorities ");
		}
		return sb.toString();
	}
}
